package com.gomore.opple.module.addcustomer;

import com.gomore.opple.data.DataRepository;
import com.gomore.opple.data.remote.retrofit.HttpResponseFunc;
import com.gomore.opple.data.remote.retrofit.RxSubscriber;
import com.gomore.opple.data.remote.retrofit.ServerResponseFunc;
import com.gomore.opple.exception.ApiException;
import com.gomore.opple.model.StoreAddress;
import com.gomore.opple.web.cgform.consumer.entity.TOConsumerEntity;
import com.gomore.opple.web.cgform.shippingaddr.entity.TOShippingAddrEntity;

import javax.inject.Inject;

import okhttp3.MultipartBody;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class AddConsumerPresenter implements AddConsumerContract.Presenter {

    private DataRepository mDataRepositroy;
    private final AddConsumerContract.View mView;
    private CompositeSubscription mSubscriptions;

    @Inject
    AddConsumerPresenter(DataRepository dataRepository, AddConsumerContract.View view) {
        mDataRepositroy = dataRepository;
        mView = view;
        mSubscriptions = new CompositeSubscription();
        mView.setPresenter(this);
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mSubscriptions.clear();
    }

    /**
     * 初始化数据
     */
    @Override
    public void prepareInitData() {
        unsubscribe();
        mView.showProgressDialog();
        String currentDepartId = mDataRepositroy.getUser().getEmployee().getCurrentDepart().getId();
        Subscription subscription = mDataRepositroy.getStoreAddress(currentDepartId)
                .map(new ServerResponseFunc<StoreAddress>())
                .onErrorResumeNext(new HttpResponseFunc<StoreAddress>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<StoreAddress>() {
                    @Override
                    public void onNext(StoreAddress storeAddress) {
                        super.onNext(storeAddress);
                        mView.hideProgressDialog();
                        mView.setStoreAddress(storeAddress);
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public void save(final TOConsumerEntity toConsumerEntity, final boolean isCreateConsumer) {
        unsubscribe();
        mView.showProgressDialog();
        final Subscription subscription = mDataRepositroy.save(toConsumerEntity)
                .map(new ServerResponseFunc<String>())
                .onErrorResumeNext(new HttpResponseFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<String>() {
                               @Override
                               public void onNext(String consumerId) {
                                   super.onNext(consumerId);
                                   mView.hideProgressDialog();
                                   //保存成功之后，保存到本地
                                   toConsumerEntity.setId(consumerId);
                                   mDataRepositroy.saveConsumer(toConsumerEntity);
                                   if (isCreateConsumer) {
                                       //保存当前客户收货地址
                                       saveAddress(toConsumerEntity, consumerId);
                                   } else {
                                       //退出
                                       mView.goBack(consumerId);
                                   }
                               }

                               @Override
                               protected void onError(ApiException ex) {
                                   mView.hideProgressDialog();
                                   mView.showMessage(ex.getMessage());
                               }
                           }

                );
        mSubscriptions.add(subscription);
    }

    @Override
    public void saveAddress(TOConsumerEntity toConsumerEntity, final String consumerId) {
        unsubscribe();
        mView.showProgressDialog();
        TOShippingAddrEntity toShippingAddrEntity = new TOShippingAddrEntity();
        toShippingAddrEntity.setConsumerId(consumerId);
        toShippingAddrEntity.setName(toConsumerEntity.getName());
        toShippingAddrEntity.setMiblephone(toConsumerEntity.getMobile());
        toShippingAddrEntity.setProvince(toConsumerEntity.getProvince());
        toShippingAddrEntity.setCity(toConsumerEntity.getCity());
        toShippingAddrEntity.setCountry(toConsumerEntity.getCountry());
        toShippingAddrEntity.setAddress(toConsumerEntity.getAddress());
        Subscription subscription = mDataRepositroy.saveShippingAddress(toShippingAddrEntity)
                .map(new ServerResponseFunc<String>())
                .onErrorResumeNext(new HttpResponseFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<String>() {
                    @Override
                    public void onNext(String string) {
                        super.onNext(string);
                        mView.hideProgressDialog();
                        //退出
                        mView.goBack(consumerId);
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                        mView.showMessage(ex.message);
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public void saveAttachment(MultipartBody.Part file) {
        unsubscribe();
        mView.showProgressDialog();
        Subscription subscription = mDataRepositroy.uploadAttachment(file)
                .map(new ServerResponseFunc<String>())
                .onErrorResumeNext(new HttpResponseFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<String>() {
                    @Override
                    public void onNext(String url) {
                        super.onNext(url);
                        mView.hideProgressDialog();
                        mView.setUrl(url);
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                        mView.showMessage(ex.getMessage());
                    }
                });
        mSubscriptions.add(subscription);
    }
}
